<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Data.mysql.php');

Class TicketData extends db
{
	function TicketData()
	{
		parent::db();
		
		$this->tableName = 'ticket';
		
		$this->AddMap("ID", "id", "id", DATATYPE_INTEGER, true);
		$this->AddMap("FormID", "form_id", "form_id", DATATYPE_INTEGER, true);
		$this->AddMap("Code", "ticketCode", "ticketCode", DATATYPE_TEXT, true);
		$this->AddMap("Name", "name", "name", DATATYPE_TEXT, false);
		$this->AddMap("Address", "address", "address", DATATYPE_TEXT, false);
		$this->AddMap("City", "city", "city", DATATYPE_TEXT, false);
		$this->AddMap("Email", "email", "email", DATATYPE_TEXT, false);
		$this->AddMap("Price", "price", "price", DATATYPE_INTEGER, false);
		$this->AddMap("Created", "created", "created", DATATYPE_DATETIME, true);
		$this->AddMap("Confirmed", "confirmed", "confirmed", DATATYPE_DATETIME, true);
		$this->AddMap("Prepared", "prepared", "prepared", DATATYPE_DATETIME, true);
		$this->AddMap("Cancelled", "cancelled", "cancelled", DATATYPE_DATETIME, true);
	}
  
	function Retrieve(&$form)
	{
//print('<pre>'.var_export($form, true).'</pre>');exit;
		$sql  = "SELECT ";
		$sql .= $this->GetFieldList();
		$sql .= "FROM " . $this->GetTableName();
		$sql .= $this->GetWhereClause($form);
		$sql .= "ORDER BY tbl.Created ";
//print($sql);exit;

		$form->Collection = array();

		$rows = $this->Select($sql);
		foreach($rows as $key=>$row)
		{
			$form->Collection[] = $this->FillRow($row);
		}
	}
	
	function GetTicketData(&$ticket)
	{
		$sql  = "SELECT fe.ticketCode, ff.name, ff.label, fe.value  ";
		$sql .= "FROM " . $this->GetTableName('formentry', 'fe', true);
		$sql .= "    , " . $this->GetTableName('formfield', 'ff', true);
		$sql .= "WHERE fe.ticketCode = '" . $ticket->Code . "' ";
		$sql .= "    AND ff.id = fe.formfield_ID ";
		$sql .= "ORDER BY ff.sortorder ";
//print($sql);exit;

		$form->Collection = array();

		$rows = $this->Select($sql);
//print('<pre>'.var_export($rows, true).'</pre>');exit;
		foreach($rows as $key=>$row)
		{
			$ticket->Data[] = array(
				'ticketCode' => $row['ticketCode'],
				'name'       => $row['name'],
				'label'      => $row['label'],
				'value'      => $row['value']);
		}
//print('<pre>'.var_export($ticket, true).'</pre>');exit;
	}
  
	function FillRow($row)
	{
		$form = new TicketModel();
		
		parent::FillRow(&$form, &$row);
		
		return $form;
	}
	
	function GetFieldList()
	{
		parent::GetFieldList(&$select);
		
		return $select;
	}

	function Update(&$ticket)
	{
		if (isset($ticket->ID) && !empty($ticket->ID) && $ticket->ID != -1)
		{
			$sql  = "UPDATE " . $this->GetTableName(false);
			$sql .= $this->GetUpdateList($ticket);
			$sql .= "WHERE id=" . $ticket->ID;
		}
		else
		{
			$sql  = "INSERT INTO " . $this->GetTableName(false);
			$sql .= $this->GetInsertFieldList();
			$sql .= $this->GetInsertValueList(&$ticket);
		}
//print($sql);exit;

	    $rows = $this->Execute($sql);
	}
  
}
?>
